#include "lrcpage.h"
#include "ui_lrcpage.h"
#include<QFile>
#include<QDebug>


LrcPage::LrcPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LrcPage)
{
    ui->setupUi(this);

    setWindowFlag(Qt::FramelessWindowHint);



    ui->hideBtn->setIcon(QIcon(":/images/xiala.png"));
    animal=new QPropertyAnimation(this,"geometry",this);
    animal->setDuration(250);


    animal->setStartValue(QRect(10,10,width(),height()));
    animal->setEndValue(QRect(10,10+height(),width(),height()));
    connect(ui->hideBtn,&QPushButton::clicked,this,[=](){
        animal->start();
    });
    connect(animal,&QPropertyAnimation::finished,this,[=]{
        hide();
    });

}

LrcPage::~LrcPage()
{
    delete ui;
}

bool LrcPage::parseLrcFile(const QString &lrcFilePath)
{
    QFile file(lrcFilePath);
    if(!file.open(QIODevice::ReadOnly)){
        qDebug()<<"打开lrc文件失败";
        return false;
    }

    lrcWordLines.clear();
    while(!file.atEnd()){
        QString lrclineWord=file.readLine(1024);

        int start=0,end=0;
        end=lrclineWord.indexOf(']',start);
        QString lrcTime=lrclineWord.mid(start,end-start+1);
        QString lrcWord=lrclineWord.mid(end+1,lrclineWord.size()-end-2);

        qint64 lineTime=0;
        start=1;
        end=lrcTime.indexOf(':',start);
        lineTime+=lrcTime.mid(start,end-start).toInt()*60*1000;

        start=end+1;
        end=lrcTime.indexOf('.',start);
        lineTime+=lrcTime.mid(start,end-start).toInt()*1000;

        start=end+1;
        end=lrcTime.indexOf('.',start);
        lineTime+=lrcTime.mid(start,end-start).toInt();

        lrcWordLines.push_back(LrcWordLine(lineTime,lrcWord));
    }
    return true;
}

void LrcPage::showLrcWordLine(qint64 time)
{
    int index=getLrcWordLineIndex(time);
    if(index==-1){
        ui->line1->setText("");
        ui->line2->setText("");
        ui->line3->setText("");
        ui->lineCenter->setText("当前歌曲无歌词");
        ui->line4->setText("");
        ui->line5->setText("");
        ui->line6->setText("");
    }
    else{
        ui->line1->setText(getLrcWordByIndex(index-3));
        ui->line2->setText(getLrcWordByIndex(index-2));
        ui->line3->setText(getLrcWordByIndex(index-1));
        ui->lineCenter->setText(getLrcWordByIndex(index));
        ui->line4->setText(getLrcWordByIndex(index+1));
        ui->line5->setText(getLrcWordByIndex(index+2));
        ui->line6->setText(getLrcWordByIndex(index+3));
    }
}

int LrcPage::getLrcWordLineIndex(qint64 time)
{
    if(lrcWordLines.isEmpty()){
        return -1;
    }
    if(time<=lrcWordLines[0].lrctime){
        return 0;
    }
    for(int i=1;i<lrcWordLines.size();i++){
        if(time>lrcWordLines[i-1].lrctime&&time<lrcWordLines[i].lrctime){
            return i-1;
        }
    }

    return lrcWordLines.size()-1;
}

QString LrcPage::getLrcWordByIndex(int index)
{
    if(index<0||index>=lrcWordLines.size()){
        return "";
    }

    return lrcWordLines[index].lrctext;
}

void LrcPage::setSinger(QString singer)
{
    QLabel*a=ui->titleBox->findChild<QLabel*>("musicSinger");
    a->setText(singer);
}

void LrcPage::setName(QString name)
{
    QLabel*a=ui->titleBox->findChild<QLabel*>("musicName");
    a->setText(name);
}
